<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2024/12/10
  Time: 11:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%@ page import="java.sql.*" %>

<%
    String content = request.getParameter("content");
    String title = request.getParameter("title");
    String userid = request.getParameter("userid");
    String text = request.getParameter("text");
    String picture = request.getParameter("picture");

    // 数据库连接信息
    String url = "jdbc:mysql://localhost:3306/db1";
    String user = "root";
    String password = "123456";

    if(title == ""){

    }else{
        try {
            // 加载驱动
            Class.forName("com.mysql.jdbc.Driver");

            // 建立连接
            Connection conn = DriverManager.getConnection(url, user, password);
//            System.out.println("conn = " + conn);

            // 创建SQL语句
            String sql = "INSERT INTO upload (CONTENT,TITLE,USERID,TEXT,PICTURE) VALUES (?,?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, content);
            pstmt.setString(2, title);
            pstmt.setString(3, userid);
            pstmt.setString(4, text);
            pstmt.setString(5, picture);
            // 执行SQL语句
            pstmt.executeUpdate();
            // 关闭连接
            pstmt.close();
            conn.close();

            out.println("内容已成功保存到数据库");
        } catch (Exception e) {
            e.printStackTrace();
            out.println("保存内容失败: " + e.getMessage());
        }
    }
%>
</body>
</html>
